package com.wangsanfei.render;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.jfinal.render.Render;
import com.wangsanfei.util.StringUtils;

public class PoiRender extends Render{

	private HSSFWorkbook wb;
	private String name;
	
	public PoiRender(HSSFWorkbook wb,String name){
		this.wb=wb;
		this.name=name;
	}
	
	public HSSFWorkbook getWb() {
		return wb;
	}
	public void setWb(HSSFWorkbook wb) {
		this.wb = wb;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public void render() {
		
		String filename="file.xls";
		
		try {
			
			if(!StringUtils.isEmpty(name)){
				filename = URLEncoder.encode(name, "UTF-8");//IE浏览器
			}
			response.addHeader("Content-disposition", "attachment; filename="+filename);
			response.setContentType("application/x-msdownload");
			
			wb.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
